We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

I wrote a simple sessionBag manager with $this->persistent , plz help me improve it

<?php

class SessionBagManager extends \Phalcon\Mvc\User\Component
{

    public function getBag($name) { 
        if (!isset($this->persistent->$name)) {
            $this->persistent->$name = new \Phalcon\Session\Bag($name);
        }
        return $this->persistent->$name;
    }
}

I don't know how to destroy a sessionbag, there is no APIs about $this->persistent



10.5k
Accepted
answer
edited Mar '15

use session instead like:

// add this to your bootstrap
$di->setShared('session', function() {
    $session = new Phalcon\Session\Adapter\Files(); // or any other adapter that you want
    $session->start();
    return $session;
});

// and your sessionBag class
class SessionBagManager extends \Phalcon\Mvc\User\Component
{
    public function getBag($name)
    { 
        if (!$this->session->has($name)) {
            $this->session->set($name, new \Phalcon\Session\Bag($name));
        }
        return $this->session->get($name);
    }

    public function destroyBag()
    {
        $this->session->destroy();
     }

     // OTHER METHODS... like remove or ....
}

see here for compelete documentation

edited Mar '15

For destroy object use unset func, you could implement other method in SessionBagManager for destroy:

public function destroyBagSession($name)
{
    //optional $this->persistent->$name = null;
    return unset($this->persistent->$name);
}


5.2k
edited Mar '15

Thanks you all. I thought that session can't used to store object reference/handler, but it can. I don't want to destroy the whole session, because other classes may use it. and for the same reason, i add a prefix. here is my improved version:

<?php

namespace Core\Session;

class SessionBagManager extends \Phalcon\Mvc\User\Component {

    private $prefix = 'bag-';

    public function getBag($name) {
        $name = $this->prefix . $name;
        if (!$this->session->has($name)) {
            $this->session->set($name, new \Phalcon\Session\Bag($name));
        }
        return $this->session->get($name);
    }

    public function removeBag($name) {
        $name = $this->prefix . $name;
        $this->session->remove($name);
    }

}

Im not sure, but if you use $this->session->destroy(); remove all session, but you must be sure. i'll do a test anyway



5.2k

yes, that's a mistake. corrected.

Im not sure, but if you use $this->session->destroy(); remove all session, but you must be sure. i'll do a test anyway



5.2k
edited Mar '15

I quickly found the code totally useless!

<?php
$bag = $this->sessionBag->getBag('upload');

$bag is only a key,value array, rather than a \Phalcon\Session\Bag instance.

but the 'persistent' way works fine.

<?php

namespace Core\Session;

class SessionBagAManager extends \Phalcon\Mvc\User\Component {

    private $prefix = 'bag-';

    public function getBag($name) {
        $name = $this->prefix . $name;
        if (!isset($this->persistent->$name)) {
            $this->persistent->$name = new \Phalcon\Session\Bag($name);
        }
        return $this->persistent->$name;

    }

    public function removeBag($name) {
        $name = $this->prefix . $name;
        unset($this->persistent->$name);
    }

}